package org.spring.springboot.service.impl;

import java.util.Date;
import java.util.Random;

import org.spring.springboot.dao.UserDao;
import org.spring.springboot.domain.User;
import org.spring.springboot.service.UserService;
import org.spring.springboot.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyuncs.exceptions.ClientException;

@Service
public class UserServiceImpl extends BaseServiceImpl<User, Integer> implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public void testTransition() throws Exception {

		User user = new User();
		user.setTelphone("15913630776");

		this.getBaseMapper().add(user);

		User user2 = new User();
		user2.setTelphone("aaa1212");

		this.getBaseMapper().add(user2);

		User user3 = new User();
		user3.setTelphone("12");

		this.getBaseMapper().add(user3);
	}

	@Override
	public void getVaildCode(String phone) throws ClientException {

		Random rand = new Random();
		int code = rand.nextInt(8999) + 1000;
		User user = userDao.findByPhone(phone);
		if (null == user) 
		{
			user=new User();
			user.setUserName("frontUser");
			user.setPassword(code+"");
			user.setTelphone(phone);
			user.setCreateDate(new Date());
			userDao.add(user);
			
		}
		else
		{
			user.setPassword(code+"");
			userDao.update(user);
		}
		
		 SmsUtil.sendSms(phone,code);

	}

	@Override
	public String doLogin(String phone, String code) {
		
		
		if(code=="")
			return "";
		
		User user = userDao.findByPhone(phone);
		
		if(user==null||!user.getPassword().equals(code))
			return "";
		else
		{
			user.setPassword("");
			userDao.update(user);
			return user.getId()+"";
		}
			
		
	}

	

	

}
